/*
 
 Copyright (c) 2010 Xiotex Studios Ltd.
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The end-user documentation included with the redistribution, if any, must
 include the following acknowledgment: "This product includes software
 developed by Xiotex Studios Ltd (http://xiotexstudios.com)", in the same
 place and form as other third-party acknowledgments. Alternately, this
 acknowledgment may appear in the software itself, in the same form and
 location as other such third-party acknowledgments.
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE. 
 
*/


#ifndef __CSWF_H__
#define __CSWF_H__


#include <string>
#include "SWFTypes.h"

// Include all the helper classes

class CSWFStream;
class CSWFTagList;
class CTexture;
class CSWFInterface;

class CSWF
{
public:
	
	typedef enum INSTRUCTION
	{
		PLACE_SPRITE,
		REMOVE_SPRITE,
		END_OF_FRAME,
	};

	typedef struct _tagCommand
	{
		INSTRUCTION		m_eInstruction;
		float			m_fScaleX;
		float			m_fScaleY;
		float			m_fAngle;
		float			m_fTX;
		float			m_fTY;
		float			m_fAlpha;
		int				m_iDepth;
		DWORD			m_dwTexture;
		float			m_fWidth;
		float			m_fHeight;
		
		float			m_fParentScaleX;
		float			m_fParentScaleY;
		float			m_fParentAngle;
		float			m_fParentTX;
		float			m_fParentTY;
		bool			m_bSetParent;
		
		char			m_cTextureName[32];
		
	} COMMAND, *PCOMMAND;
	
	
	typedef struct _tagDisplayItem
	{
		DWORD			m_dwTexture;
		float			m_fWidth;
		float			m_fHeight;
		PCOMMAND		m_pCommand;
		
		float			m_fX;
		float			m_fY;
		float			m_fX1;
		float			m_fY1;
		
		float			m_fScaleX;
		float			m_fScaleY;
		float			m_fAngle;
		float			m_fTX;
		float			m_fTY;
		
	} DISPLAY_ITEM, *PDISPLAY_ITEM;

public:
	CSWF();
	~CSWF();
	bool Initialise(std::string strPath, CSWFInterface* pInterface);
	void Shutdown();
	void Reset();
	void Play(float fTimeDelta);
	void Render(float fCentreX, float fCentreY, float fScale, float fAngle, bool bFlipX, bool bFlipY);

protected:
	void PlayFrame();
	void Transform(float fX, float fY, float fTX, float fTY, float fCentreX, float fCentreY, float fScaleX, float fScaleY, float fAngle, bool bFlipX, bool bFlipY, float fWorldAngle, float fWorldScale, float* fXOut, float* fYOut); 
	
protected:
	CSWFStream*						m_pStream;
	CSWFTagList*					m_pTagList;
	CSWFInterface*					m_pInterface;

	// Movie information
	DWORD							m_dwFlashVersion;
	DWORD							m_dwSizeOfFile;
	DWORD							m_dwFrameRate;
	DWORD							m_dwFrameCount;
	DWORD							m_dwIndexToStartOfMovie;
	DWORD							m_dwSize;
	SWF_RECT						m_rMovieDimensions;
	
	float							m_fHalfWidth;
	float							m_fHalfHeight;

	float							m_fTime;
	float							m_fDelta;
	float							m_fAccumulator;
	
	// Command stream
	std::vector<PCOMMAND>			m_vCommands;
	std::vector<PCOMMAND>::iterator	m_it;
	
	// The display list - making it 30 deep.
	int								m_iHighestLayer;
	DISPLAY_ITEM					m_vDisplayList[30];
	


};

#endif